class Food{
  constructor(options){
    //食物大小
    this.width = options.width||20;
    this.height = options.height||20;
    //食物颜色
    this.backgroundColor = "#66ccff";
    //食物位置
    this.left = options.left || 0;
    this.top = options.top || 0;
    this.elements = [];
  }
  //随机位置
  getRandom(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值，含最小值
  }
  //创建并渲染
  create(box){
    let food = document.createElement("div");
    food.style.width = this.width + "px";
    food.style.height = this.height + "px";
    food.style.backgroundColor = this.backgroundColor;
    food.style.position = "absolute";
    food.style.left = this.getRandom(0, box.clientWidth / this.width - 1) * this.width + "px";
    food.style.top =this.getRandom(0, box.clientHeight / this.height - 1) * this.height + "px";
    box.appendChild(food);
    this.elements.push(food);
  }

  //删除方块
  remove(box,i){
    box.removeChild(this.elements[i]);
    // 将元素 从 数组中删除
    this.elements.splice(i, 1);
  }
}
